<?php
namespace Mob \ Model;

use Think \ Model;
use Think \ Hook;
/**请假模型
 * Class InfoModel
 */

require_once ('./Application/Mob/Common/function.php');

class LeaveModel extends Model {

      protected $tableName = 'leaverecord';
      /**
	 * addLeave  创建记录
	 */

      public function addLeave($applyUid,$reasonType,$describe,$applyStartTime,$applyEndTime,$studentId,$classId,$contact,$schooId='10183')
      {
      $data['serialno']=getSerialno();
      $data['applyUid']=$applyUid;
      $data['applyTime']=time();
      $data['reasonType']=$reasonType;
      $data['describe']=$describe;
      $data['studentId']=$studentId;
      $data['classId']=$classId;
      $data['schooId']=$schooId;
      $data['applyStartTime']=$applyStartTime;
      $data['applyEndTime']=$applyEndTime;
      $data['contact']=$contact;
      $data['approvalUid']= $this->get_LeaveApprovalUid($classId,$schooId);
      if($this->add($data))
      {
      return $data['serialno'];
      }

      }
     /**
	 * addLeave  取个人请假记录
	 */

     public function get_LeaveRecord($applyUid,$studentId,$classId,$schooId='10183')
     {
      $where = array('applyUid'=>$applyUid,'studentId'=>$studentId,'classId'=>$classId,'schooId'=>$schooId);

      return $this->where($where)->field('serialno,applyUid,applyTime,reasonType,describe,studentId,classId,schooId,applyStartTime,applyEndTime,approvalUid,approvalTime,approvalFlg,comment,contact')->order('applyStartTime desc ')->select();


     }
     /**
	 * addLeave  取请假审批人
	 */


     public function get_LeaveApprovalUid($classId,$schooId='10183')
     {

     $orgaInfo =D('Orga')->get_OrgaInfo($classId);
     if($orgaInfo)
     {
      if(empty($orgaInfo['teacherId']))  //teacherId
      {
      return $orgaInfo['leaveApprovalUid'];     //teacherId

      }else
      {
      return $orgaInfo['teacherId'];
      }
     }

     }

      /**
	 * addLeave  取可审批班组
	 */
     public function get_ApprovalClassId($teacherId,$schoolId)
     {
      $leave = new \ Think \ Model();

    return $leave->query("select t.teacherUid,teacheName,a.orgaId from ocenter_orga a,ocenter_teacher t where  a.schoolId = t.schooId  and a.schoolId =  $schoolId and ((a.orgaType =3 and a.teacherId =$teacherId and a.leaveApprovalUid is null) or a.leaveApprovalUid = $teacherId) and t.status = 1") ;

     }

    /**
	 * addLeave  审批操作
	 */
    public function  approvalOperate($serialno,$applyUid,$approvalUid,$approvalFlg,$comment='')
    {
    $where = array('serialno'=>$serialno,'applyUid'=>$applyUid,'approvalFlg'=>0);

    $data['realityApprovalUid']=$approvalUid;  //更新为实际审批人
    $data['comment']=$comment;  //审核回复
    $data['approvalFlg']=$approvalFlg;  //审核结果
    $data['approvalTime']=time();    //审核时间
    return $this->where($where)->save($data);

    }
     public function is_ExistLeave($studentId,$schooId='10183',$applyStartTime)
 //   public function is_ExistLeave($studentId,$schooId='10183',$applyStartTime,$applyEndTime)
    {
    $start=$applyStartTime?$applyStartTime:mktime(0,0,0,date('m'),date('d'),date('Y'));
   // $end=$applyEndTime?$applyEndTime:mktime(23,59,59,date('m'),date('d'),date('Y'));
    $where = array('applyUid'=>$studentId,'schooId'=>$schooId,'applyStartTime'=>array('elt',$start),'applyEndTime'=>array('egt',$start));
    return $this->where($where)->field('applyStartTime,applyEndTime,approvalFlg')->find();


    }


    public function personLeaveRecord($uid,$schooId,$start,$end)
    {
    $start = empty($start)?mktime(0,0,0,date('m'),date('d')-90,date('Y')):strtotime($start);  //开始时间
    $end  = empty($end)?mktime(0,0,0,date('m'),date('d')+30,date('Y')):strtotime($end);       //结束时间
    $where = array('applyUid'=>$uid,'schooId'=>$schooId,'applyStartTime'=>array('egt',$start),'applyEndTime'=>array('elt',$end));

    return  $this->where($where)->field('serialno,applyUid,applyTime,describe,studentId,classId,schooId,applyStartTime,applyEndTime,approvalUid,approvalTime,approvalFlg,comment,contact')->order('applyStartTime desc ')->select();
    }

    public function get_LeaveDetails($serialno)
    {
    	$where = array('serialno'=>$serialno);
     $res = $this->where($where)->field("serialno,applyUid,applyTime,describe,studentId,classId,schooId,FROM_UNIXTIME(applyStartTime,'%Y-%m-%d') as applyStartTime,FROM_UNIXTIME(applyEndTime,'%Y-%m-%d') as applyEndTime,approvalUid,approvalTime,approvalFlg,comment,FROM_UNIXTIME(applyTime,'%Y%m%d') as applyDate,CONVERT((applyEndTime-applyStartTime)/86400,SIGNED) as applyDay,contact")->find();
     return $res;



    }

    public function personLeaveRecordByDate($uid,$schooId='10183',$start='',$end='')
    {
    $result = $this->personLeaveRecord($uid,$schooId,$start,$end);
    $leaveRecord = array();
    foreach($result as $r)
    {
     for($i=$r['applyStartTime']; $i<$r['applyEndTime'];$i+=(24*3600))
    {
    array_push($leaveRecord,array('date'=>date('Y-m-d',$i),'approvalFlg'=>$r['approvalFlg'],'serialno'=>$r['serialno']));
    }
    }

   return $leaveRecord;


    }

    public function authstrLeaveList ($teacherId,$schooId)
    {
     $leave = new \ Think \ Model();
   return  $leave->query(" select d.serialno,d.applyUid,d.applyTime,d.reasonType,d.describe,d.studentId,o.orgaid as classId,d.schooId,o.teacherId,o.leaveApprovalUid from ocenter_leaverecord d,(select
orgaid,teacherId,leaveApprovalUid from ocenter_orga  where  schooId = $schooId and  (leaveApprovalUid = $teacherId or teacherId = $teacherId )  and status =1) o where d.classid = o.orgaid  and d.schooId = $schooId and
d.approvalFlg = 0 order by d.applyTime");

  //  $where = array('approvalUid'=>$teacherId,'schooId'=>$schooId,'approvalFlg'=>0);
   // return $this->where($where)->field('serialno,applyUid,applyTime,reasonType,describe,studentId,classId,schooId')->select();


    }

    public function get_ClassLeaveRecord($classId,$schooId)
    {
     $where = array('classId'=>$classId,'schooId'=>$schooId,'approvalFlg'=>array('neq',0));
     return $this->where($where)->field('serialno,applyUid,applyTime,describe,studentId,classId,schooId,applyStartTime,applyEndTime,approvalUid,approvalTime,approvalFlg,comment,contact')->select();


    }

    public function get_approvalhistory($teacherId,$schooId)
    {
    /*
    $where = array('approvalUid'=>$teacherId,'schooId'=>$schooId,'approvalFlg'=>array('neq',0));
    $res = $this->where($where)->field('serialno,applyUid,applyTime,describe,studentId,classId,schooId,applyStartTime,applyEndTime,approvalUid,approvalTime,approvalFlg,comment')->order('approvalTime desc')->select();
    */
     $leave = new \ Think \ Model();
    $res =  $leave->query("select d.serialno,d.applyUid,s.name,d.applyTime,d.describe,d.studentId,d.classId,d.schooId,d.applyStartTime,d.applyEndTime,d.approvalUid,d.approvalTime,d.approvalFlg,d.comment,FROM_UNIXTIME(d.applyStartTime,'%Y-%m-%d') as applyStartDate ,FROM_UNIXTIME(d.applyEndTime,'%Y-%m-%d') as applyEndDate,d.contact  from ocenter_leaverecord d,ocenter_student s where d.approvalUid = $teacherId and d.schooId = $schooId and approvalFlg <>0
and d.applyUid = s.Uid order by d.approvalTime desc");


    $orga = D('Orga');
    foreach($res as &$v)
    {
    $v['OrgaInfo'] = $orga->get_OrgaFullInfo($v['classId'],$v['schooId']);
    }
    unset($v);
   return $res;

    }

    public function sendRemind($serialno,$type)
    {
    $leaveInfo = $this->get_LeaveDetails($serialno);
    if(empty($leaveInfo))
    {
    return false;
    }

    switch($type)
    {

    case 1: ///提交审请
    $receiveUid = $leaveInfo['approvalUid'];       //设置接收人为对应的老师
    $name = D('Student')->queryStudent($leaveInfo['applyUid'])['name'];
    $reason = $leaveInfo['describe'];

     $data = array( 'first'=>array('value'=>urlencode('请假申请审批'),'color'=>"#743A3A"),
                'keyword1'=>array('value'=>urlencode($name),'color'=>'#173177'),
                'keyword2'=>array('value'=>urlencode(date('Y-m-d H:i:s',time())),'color'=>'#173177'),
                'keyword3'=>array('value'=>urlencode('原因:'.$reason),'color'=>'#173177'),
               // 'remark'=>array('value'=>urlencode('你说的对啊!'),'color'=>'#FFFFFF'),
            );



    break;

    case 2:  //审批回复
    $receiveUid = $leaveInfo['applyUid'];       //设置接收人为对应学生
    $TeacherName = 	D('Teacher')->query_Teacher($leaveInfo['approvalUid'])['teacheName'];
    if($leaveInfo['approvalFlg']==1)
    {
    $result = '请假批准';
    }elseif($leaveInfo['approvalFlg']==2)
    {
    $result = '请假拒绝';
    }
     $data = array( 'first'=>array('value'=>urlencode('请假结果通知'),'color'=>"#743A3A"),
                'keyword1'=>array('value'=>urlencode("审批人:".$TeacherName),'color'=>'#173177'),
                'keyword2'=>array('value'=>urlencode(date('Y-m-d H:i:s',time())),'color'=>'#173177'),
                'keyword3'=>array('value'=>urlencode('结果:'.$result),'color'=>'#173177'),
               // 'remark'=>array('value'=>urlencode('你说的对啊!'),'color'=>'#FFFFFF'),
            );



    break;




    }

    $srcUid = 'sys';
    $res = D('WxMessage')->sendMessage($srcUid,$receiveUid,1,$data);
    if($res)
    {
    return true;
    }

   }

   public function check_ApprovalAuth($Serialno,$teacherId,$schooId='10183')
   {

   	$leave = $this->where("serialno = $Serialno")->field("serialno,applyUid,applyTime,describe,studentId,classId,schooId,FROM_UNIXTIME(applyStartTime,'%Y-%m-%d') as applyStartTime,FROM_UNIXTIME(applyEndTime,'%Y-%m-%d') as applyEndTime,approvalUid,approvalTime,approvalFlg,comment,FROM_UNIXTIME(applyTime,'%Y%m%d') as applyDate,CONVERT((applyEndTime-applyStartTime)/86400,SIGNED) as applyDay,contact")->find();
    $classId = $leave['classId'];
   	$orgaInfo = M('Orga')->where("schooId = $schooId and OrgaId = $classId and orgaType =3 and status =1")->field('teacherId,leaveApprovalUid')->find();

   	if($orgaInfo['leaveApprovalUid']==$teacherId||$orgaInfo['teacherId']==$teacherId)
   	{
   	return $leave;
   	}





   }















}